/*
 * @lc app=leetcode id=209 lang=cpp
 *
 * [209] Minimum Size Subarray Sum
 */
class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {
        int len = nums.size();
        if (len < 1) {
            return 0;
        }

        int l = 0, r = 0;
        int minLen = INT_MAX;

        int sum = 0;
        while (r < len) {
            sum += nums[r];
            while (l <= r && sum >= s) {
                if (r-l+1 < minLen) {
                    minLen = r-l+1;
                }
                sum -= nums[l++];
            }
            r++;
        }
        return minLen < INT_MAX ? minLen : 0;
    }
};

